1 00:00:00,440 --> 00:00:01,430 Welcome back. 2 00:00:01,430 --> 00:00:05,690 In this lecture we're going to take a look at a concept called abstraction. 3 00:00:05,750 --> 00:00:10,820 Abstraction is the idea of hiding unnecessary details from the user of a class. 4 00:00:10,820 --> 00:00:16,430 Any functions or variables that exist in the class for the sole purpose of only working internally should 5 00:00:16,430 --> 00:00:19,730 be hidden away from view, and should not be visible to the user. 6 00:00:19,760 --> 00:00:25,220 As an example, think of a laptop and how its manufacturers allow you to interact with it using only 7 00:00:25,220 --> 00:00:31,130 the keyboard and the trackpad, and they hide all the internal working components within a shroud or 8 00:00:31,130 --> 00:00:32,660 cover around the laptop. 9 00:00:32,690 --> 00:00:36,800 This is the same idea we should uphold for when we create classes and module scripts. 10 00:00:36,800 --> 00:00:42,290 Any functions or variables that do not need to be accessed outside of the module script should be hidden 11 00:00:42,290 --> 00:00:42,860 away. 12 00:00:43,280 --> 00:00:46,100 So let's go ahead and get started scripting with an example. 13 00:00:46,100 --> 00:00:48,110 I'm going to create a new class. 14 00:00:48,110 --> 00:00:50,210 This class is going to be called rectangle. 15 00:00:53,450 --> 00:00:59,000 And some basic properties that a rectangle can have is a position on the x axis, a position on the 16 00:00:59,000 --> 00:01:04,850 y axis, and then maybe the width and the height of the rectangle. 17 00:01:05,300 --> 00:01:10,610 And then from this point we can set the underscore underscore index meta method equal to the rectangle 18 00:01:10,610 --> 00:01:11,330 itself. 19 00:01:12,060 --> 00:01:15,330 And then let's create a constructor for the rectangle class. 20 00:01:16,590 --> 00:01:20,850 Will pass the x, the y, the width and the height. 21 00:01:21,300 --> 00:01:23,760 And then from here we'll create a new table. 22 00:01:23,760 --> 00:01:31,170 From the set meta table function, attach the rectangle table to it, and then we can set all of the 23 00:01:31,170 --> 00:01:32,820 properties accordingly. 24 00:01:41,480 --> 00:01:45,020 And we'll make sure to return self back to where this function was called. 25 00:01:45,710 --> 00:01:51,260 And from this point, maybe we want to have a function to calculate the area for a given rectangle. 26 00:01:51,260 --> 00:01:53,000 So we can do rectangle. 27 00:01:53,030 --> 00:01:54,980 Call this function get area. 28 00:01:55,340 --> 00:01:58,700 And what we can do is return the area of this rectangle. 29 00:01:58,730 --> 00:02:01,340 But let's say I want to calculate the area internally. 30 00:02:01,340 --> 00:02:05,840 What we could do is create a function I'm going to call it calculate area. 31 00:02:06,840 --> 00:02:14,280 Pass the rectangle we want to calculate the area for and then return the self dot width multiplied by 32 00:02:14,280 --> 00:02:15,750 the self dot height. 33 00:02:16,930 --> 00:02:19,240 And then we can call the function here. 34 00:02:21,240 --> 00:02:23,550 And pass the rectangle. 35 00:02:23,850 --> 00:02:28,320 So this function right here is hidden away inside of this module script. 36 00:02:28,320 --> 00:02:32,550 Any script that requires this module script is not going to see this function. 37 00:02:32,550 --> 00:02:35,280 We're upholding the concept of abstraction here. 38 00:02:35,280 --> 00:02:37,710 We're hiding away the unnecessary details. 39 00:02:37,710 --> 00:02:42,210 The only function we want to see is the get area function, which is just going to return to us a number. 40 00:02:42,210 --> 00:02:46,650 But actually calculating the number in this local function, we don't want to see because we don't care 41 00:02:46,650 --> 00:02:47,160 about it. 42 00:02:47,160 --> 00:02:51,000 The only thing we care about is interacting with the class itself with these functions. 43 00:02:51,840 --> 00:02:55,680 And then at the very end of our module script, we can return this rectangle table. 44 00:02:55,980 --> 00:02:58,650 And then we can go into this other script that I have here. 45 00:02:58,890 --> 00:03:01,350 I'm going to make a reference to server storage. 46 00:03:05,160 --> 00:03:08,070 And then I'm going to access my rectangle class. 47 00:03:09,720 --> 00:03:11,250 Which is in server storage. 48 00:03:11,250 --> 00:03:13,650 I have a folder in there called classes. 49 00:03:13,830 --> 00:03:18,090 And inside of there that is where my rectangle module script exists. 50 00:03:19,620 --> 00:03:25,890 And then from this point I can create a new rectangle using the rectangle dot new function, give it 51 00:03:25,890 --> 00:03:30,900 a position of zero and zero, and we can give it a width of ten and a height of 20. 52 00:03:31,920 --> 00:03:37,680 And then from this point, what we could do is that we could get the area for our rectangle. 53 00:03:39,730 --> 00:03:42,340 And we can store in any variable called area. 54 00:03:43,700 --> 00:03:50,600 Now, as you can see, when I index my new rectangle, I'm only able to access the properties that exist 55 00:03:50,600 --> 00:03:52,400 within this class. 56 00:03:52,400 --> 00:03:57,950 Here I can only access the functions, and I can access the properties that exist within my rectangle. 57 00:03:57,950 --> 00:04:00,890 However, I cannot access this function here. 58 00:04:00,890 --> 00:04:02,000 I can't see it. 59 00:04:02,120 --> 00:04:07,520 However, this would be different if I made this function a part of the rectangle class. 60 00:04:07,520 --> 00:04:12,800 So if I did this instead, when I go to index my new rectangle, I can see my function appear right 61 00:04:12,800 --> 00:04:13,400 here. 62 00:04:14,000 --> 00:04:18,650 However, since it's working only internally within our module script, we don't want to be able to 63 00:04:18,650 --> 00:04:21,110 see it here because it doesn't matter to us. 64 00:04:21,110 --> 00:04:25,460 That's why we make this function only belong to the module script itself. 65 00:04:25,460 --> 00:04:26,720 And it's local. 66 00:04:26,720 --> 00:04:28,280 We're hiding it away. 67 00:04:30,420 --> 00:04:32,850 And now I can print the area from our rectangle. 68 00:04:32,850 --> 00:04:40,710 I can just print this rectangle has an area of and concatenate it with the area. 69 00:04:40,980 --> 00:04:44,430 We can go and run our game and we should get an area of 200. 70 00:04:44,430 --> 00:04:45,120 There we go. 71 00:04:45,120 --> 00:04:47,760 This rectangle has an area of 200. 72 00:04:48,760 --> 00:04:53,020 So the basic idea of abstraction is just to hide any unnecessary details. 73 00:04:53,020 --> 00:04:56,290 We don't need to see when we're interacting with our class. 74 00:04:56,830 --> 00:04:59,350 Hope you learned a lot and I'll see you in the next lecture.